package com.senjoeson.socialsecure.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.senjoeson.socialsecure.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author senjoeson
 * @date 2018/9/16 21:51
 */
public class WebviewActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    public TextView mToolBarTitle;
    @BindView(R.id.webview)
    WebView mWebView;
    public Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mUnbinder = ButterKnife.bind(this);
        setSupportActionBar(mToolbar);
        mToolBarTitle = mToolbar.findViewById(R.id.toolbar_title);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setWebView();

        String url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title")+"";
        mToolBarTitle.setText(title);
        if (url != null) {
            mWebView.loadUrl(url);
        }


    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setWebView() {
        WebSettings webSettings = mWebView.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webSettings.setUseWideViewPort(true);//关键点

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        webSettings.setDisplayZoomControls(false);

        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本

        webSettings.setAllowFileAccess(true); // 允许访问文件

        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮

        webSettings.setSupportZoom(true); // 支持缩放

        webSettings.setLoadWithOverviewMode(true);

        DisplayMetrics metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int mDensity = metrics.densityDpi;

        Log.d("maomao", "densityDpi = " + mDensity);

        if (mDensity == 240) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        } else if (mDensity == 160) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

        } else if(mDensity == 120) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);

        }else if(mDensity == DisplayMetrics.DENSITY_XHIGH){

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        }else if (mDensity == DisplayMetrics.DENSITY_TV){

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        }else{

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

        }
        mWebView.setWebChromeClient(new WebChromeClient());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mWebView != null) {
            mWebView.goBack();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.clearFormData();
            mWebView.clearHistory();
        }
        if(mUnbinder!=null){
            mUnbinder.unbind();
        }
    }
}
